<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    //显示商品列表
    public function index(){
        $goods = DB::table('goods')->paginate(5);
        return view('admin.goods.index', ['goods' => $goods]);
    }

    //添加商品界面
    public function add(){
        $types=DB::table('gtype')->get();
        return view('admin.goods.add',['types'=>$types]);
    }

    //添加商品操作
    public function insert(Request $request){
        //判断是否有上传文件
        if(!$request->hasFile('gpic')){
            return error('/admin/goods/add','请上传商品图片',3);
        }

        //生成文件夹
        $dir=date('Y-m-d');
        //生成文件名
        $fileName=uniqid().'.jpg';
        //组合上传路径存储到数据库中
        $path=$dir.'/'.$fileName;
        //移动上传文件夹
        $request->file('gpic')->move($dir,$fileName);

        //获取所有传参信息
        $gname=$request->input('gname');
        $tname=$request->input('tname');

        //查询是否已存在该商品
        $row=DB::table('goods')->where('gname','=',$gname)->where('stock','>',0)->first();
        //根据结果判断商品是否已存在
        if($row){
            return error('/admin/goods/add','商品已存在',3);
        }else{
            //获取插入信息
            $good=$request->only(['gname','price','stock','ginfo']);
            $good['gpic']=$path;
            $good['tid']=$tname;
            //将商品插入数据库
            $result=DB::table('goods')->insert($good);
            //判断商品是否插入成功
            if($result){
                return success('/admin/goods/index','插入商品成功',3);
            }else{
                return error('/admin/goods/add','插入商品失败',3);
            }
        }
    }

    //删除商方法
    public function delete(Request $request){
        //接收用户id
        $gid=$request->input('gid');
        //删除操作
        $result=DB::table('goods')->where('gid','=',$gid)->delete();

        if($result){
            echo 1;
        }else{
            echo 0;
        }
    }

    //修改商品方法
    public function modify(Request $request){
        //获取用户id
        $gid=$request->input('gid');
        //查询用户信息
        $good=DB::table('goods')->where('gid','=',$gid)->first();
        //json编码后返回
        return response()->json($good);
    }

    //修改商品操作
    public function update(Request $request){
        //判断是否有上传文件
        if(!$request->hasFile('gpic')){
            $good=$request->only(['gname','price','stock','ginfo','tid']);
        }else{
            //生成文件夹
            $dir=date('Y-m-d');
            //生成文件名
            $fileName=uniqid().'.jpg';
            //组合上传路径存储到数据库中
            $path=$dir.'/'.$fileName;
            //移动上传文件夹
            $request->file('gpic')->move($dir,$fileName);

            $good=$request->only(['gname','price','stock','ginfo','tid']);
            $good['gpic']=$path;
        }

        $gid=$request->input('gid');
        $result=DB::table('goods')->where('gid','=',$gid)->update($good);

        if($result){
            return success('/admin/goods/index','更新用户信息成功',1);
        }else if($result==0){
            return success('/admin/goods/index','你啥都不改,是来逗我的吗?',3);
        }else{
            return error('/admin/goods/index','更新用户信息失败',3);
        }
    }

    //查找商品界面
    public function find(){
        return view('admin.goods.find');
    }

    //查找商品操作
    public function search(Request $request){
        $keywords=$request->input('key');
        $goods=DB::table('goods')
            ->where('gid','=',$keywords)
            ->orwhere('gname','like','%'.$keywords.'%')
            ->orwhere('stock','like','%'.$keywords.'%')
            ->orwhere('price','like','%'.$keywords.'%')
            ->orwhere('ginfo','like','%'.$keywords.'%')
            ->paginate(3);
        return view('admin.goods.index', ['goods' => $goods]);
    }

    //查找商品界面
    public function stockfind(){
        return view('admin.goods.stockfind');
    }

    //查找商品操作
    public function stocksearch(Request $request){
        $keywords=$request->input('key');
        $goods=DB::table('goods')
            ->where('stock','=',$keywords)
            ->orwhere('gname','like','%'.$keywords.'%')
            ->paginate(5);
        return view('admin.goods.index', ['goods' => $goods]);
    }
	
	//------------------------------------------------------------------
	
	
	
}

